package edu.vt.ece_3574.amber_alert;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import edu.vt.ece_3574.amber_alert.AlertSettings.AlertSettings;
import edu.vt.ece_3574.amber_alert.accounts.Account;
import edu.vt.ece_3574.amber_alert.curfew.CurfewCreationActivity;
import edu.vt.ece_3574.amber_alert.database.DatabaseHandler;
import edu.vt.ece_3574.amber_alert.membership.AccountSettingsLoginActivity;
import edu.vt.ece_3574.amber_alert.membership.MemberAdderActivity;

public class PrimaryMain extends Activity {
	public static String target;
	
	Spinner memberSelectorSpinner;
	
	public static Account selectedMember;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.primary_main);
		
		ensureMemberRegistered();
		populateSpinner();
		
		selectedMember = DatabaseHandler.get().getAccountFromServerByName((String) memberSelectorSpinner.getSelectedItem());

		Button locateMyKid = (Button) findViewById(R.id.locateMyKidButton);
		locateMyKid.setOnClickListener(locationButtonListener);
		
		//Set the OnClickListener for the Set Curfew button to start the CurfewCreationActivity activity
		Button setCurfewButton = (Button) findViewById(R.id.setCurfewButton);
		setCurfewButton.setOnClickListener(curfewButtonListener);
		
		Button myaccount = (Button) findViewById(R.id.myaccount);
		myaccount.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
					Intent intent = new Intent(PrimaryMain.this, AccountSettingsLoginActivity.class);
					startActivity(intent);
					finish();
			}
		});
        
        Button logoutButton = (Button)findViewById(R.id.logoutButton);
        logoutButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				eraseRememberedAccount();
				Intent i = new Intent(PrimaryMain.this, LoginActivity.class);
				startActivity(i);
				PrimaryMain.this.finish();
			}
        });
	
	
	 Button alertsettings = (Button)findViewById(R.id.alertSettings);
     alertsettings.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(PrimaryMain.this, AlertSettings.class);
				startActivity(i);
				finish();
			}
     });
	}
	
	private void eraseRememberedAccount() {
		SharedPreferences settings = getSharedPreferences(AmberAlertApplication.preferencesFileName, 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString("rememberedAccountName", "AmberAlertAccountNotSet");
		editor.commit();
	}
	
	private View.OnClickListener curfewButtonListener = new OnClickListener() {
		public void onClick(View v) {
			selectedMember = DatabaseHandler.get().getAccountFromServerByName((String) memberSelectorSpinner.getSelectedItem());
			Intent intent = new Intent(PrimaryMain.this, CurfewCreationActivity.class);
			startActivity(intent);
			finish();
		}
	};
	
	private View.OnClickListener locationButtonListener = new OnClickListener() {
		public void onClick(View v) {
			selectedMember = DatabaseHandler.get().getAccountFromServerByName((String) memberSelectorSpinner.getSelectedItem());
			DatabaseHandler.get().displayAccountLocation(PrimaryMain.this, selectedMember);
		}
	};
	
	private void populateSpinner() {
		memberSelectorSpinner = (Spinner) findViewById(R.id.memberSelectorSpinner);
        //Create an ArrayAdapter which will handle adding all of the accounts to the spinner
        ArrayAdapter <CharSequence> adapter = new ArrayAdapter <CharSequence> (this, android.R.layout.simple_spinner_item );
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //Add each account to the ArrayAdapter
        for (String memberName : AmberAlertApplication.get().getCurrentAccount().getAssociatedAccountNames()) {
        	adapter.add(memberName);
        }
        		 
        //Populate the spinner with the members contained in the ArrayAdapter
        memberSelectorSpinner.setAdapter(adapter);
	}
	
	private void ensureMemberRegistered() {
		if (AmberAlertApplication.get().getCurrentAccount().getAssociatedAccountNames().size() == 0) {
			Intent i = new Intent(PrimaryMain.this, MemberAdderActivity.class);
			startActivity(i);
			PrimaryMain.this.finish();
		}
	}
}
